/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.modules.biz.domain.Sensor;
import me.zhengjie.modules.biz.service.SensorService;
import me.zhengjie.modules.biz.service.dto.SensorQueryCriteria;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Admin
* @date 2023-02-17
**/
@RestController
@RequiredArgsConstructor
@EnableScheduling
@Api(tags = "sensor管理")
@RequestMapping("/api/sensor")
public class SensorController implements CommandLineRunner {

    private final SensorService sensorService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('sensor:list')")
    public void exportSensor(HttpServletResponse response, SensorQueryCriteria criteria) throws IOException {
        sensorService.download(sensorService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询sensor")
//    @PreAuthorize("@el.check('sensor:list')")
    public ResponseEntity<Object> querySensor(SensorQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(sensorService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增sensor")
    @ApiOperation("新增sensor")
//    @PreAuthorize("@el.check('sensor:add')")
    public ResponseEntity<Object> createSensor(@Validated @RequestBody Sensor resources){
        return new ResponseEntity<>(sensorService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改sensor")
    @ApiOperation("修改sensor")
//    @PreAuthorize("@el.check('sensor:edit')")
    public ResponseEntity<Object> updateSensor(@Validated @RequestBody Sensor resources){
        sensorService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping("delete")
    @Log("删除sensor")
    @ApiOperation("删除sensor")
//    @PreAuthorize("@el.check('sensor:del')")
    public ResponseEntity<Object> deleteSensor(@RequestBody String[] createdAt) {
        sensorService.deleteAll(createdAt);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    //@Scheduled(cron ="*/5 * * * * ?")
    @PostMapping(value = "/real")
    public void realTimeUpdateSenor(){
        sensorService.realTimeUpdate();
    }

    @Override
    public void run(String... args) throws Exception {
//        realTimeUpdateSenor();
    }


    @GetMapping("/querySensorByType")
//    @PreAuthorize("@el.check('sensor:list')")
    public ResponseEntity<Object> querySensorByType(SensorQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(sensorService.querySensorByType(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping("/setAlarmThreshold")
    public ResponseEntity<Object> setAlarmThreshold(@RequestParam String ip, @RequestParam String address, @RequestParam Float alarmThreshold){
        sensorService.setAlarmThreshold(ip, address, alarmThreshold);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/3d/listFloor")
    @AnonymousAccess
    public ResponseEntity<Object> listFloor(Integer floor){
        return new ResponseEntity<>(sensorService.listFloor(floor),HttpStatus.OK);
    }

    @AnonymousGetMapping("/3d/getCountByFloor")
    public ResponseEntity<Object> getCountByFloor(){
        return new ResponseEntity<>(sensorService.getCountByFloor(),HttpStatus.OK);
    }
}
